﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aij1.Model;
using Aij1.Utils;
using Aij1.BLL;

namespace Aij1.Cms
{
    public partial class News : AdminPage
    {
        protected A_Article A;
        A_Category Category;
        IList<A_Category> categorys;
        IList<ArticleStatusType> statusTypes;
        protected void Page_Load(object sender, EventArgs e)
        {
            int catId = CommOp.ToInt(Request.QueryString["CatId"]);

            if (catId > 0) Category = XpoSession.GetObjectByKey<A_Category>(catId);

            int artId = CommOp.ToInt(Request["Id"]);
            if (artId > 0)
            {
                A = XpoSession.GetObjectByKey<A_Article>(artId);
                Category = A.CategoryId;
            }
            else
            {
                A = ArticleData.CreateNewArticle(XpoSession);
            }
            categorys = CategoryData.GetCategorys(XpoSession, ArticleType.新闻);
            cbxCategory.DataSource = categorys;
            cbxCategory.DataBind();
            statusTypes = CategoryData.GetTypes<ArticleStatusType>();
            cbxStatus.DataSource = statusTypes;
            cbxStatus.DataBind();
            edtContent.SettingsImageUpload.UploadImageFolder = Config.UploadDir;
            if (!IsPostBack)
            {
                AssignArticles();
            }
        }

        void AssignArticles()
        {
            txtTitle.Text = A.Title;
            txtIntro.Text = A.Intro;
            txtAuthor.Text = A.Author;
            edtContent.Html = A.Content;
            txtKeywords.Text = A.Keywords;
            dtDisplayTime.DateTime = A.DisplayTime;
            txtSourceUrl.Text = A.SourceUrl;
            if (Category != null) cbxCategory.Text = Category.Title;
            cbxStatus.Value = A.Status;
        }

        void GetArticles()
        {
            A.Title = txtTitle.Text.Trim();
            A.Intro = txtIntro.Text.Trim();
            A.Author = txtAuthor.Text.Trim();
            A.Content = edtContent.Html.Trim();
            A.DisplayTime = dtDisplayTime.DateTime;
            A.EditorId = AuthData.GetCurrentUser(XpoSession);
            A.EditTime = DateTime.Now;
            A.Keywords = txtKeywords.Text.Trim().Replace(",", ";");
            A.SourceUrl = txtSourceUrl.Text.Trim();

            if (cbxCategory.SelectedIndex >= 0)
            {
                A.CategoryId = categorys[cbxCategory.SelectedIndex];
            }
            if (cbxStatus.SelectedIndex >= 0)
            {
                A.Status = statusTypes[cbxStatus.SelectedIndex];
            }
        }

        protected void ASPxCallback1_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
        {
            GetArticles();
            switch (e.Parameter)
            {
                case "pick":
                    e.Result = ArticleData.PickKeyword(A);
                    break;
                default:
                    ArticleData.SaveImage(A, chkSaveToLocal.Checked);
                    A.Session.Connect();
                    A.Save();
                    e.Result = "保存成功！";
                    break;
            }
        }

        protected void ASPxCallback1_CustomJSProperties(object source, EventArgs e)
        {
            if (A != null)
                ASPxCallback1.JSProperties["cpNewsId"] = A.Oid;
        }
    }
}